<?php
/**
 * Created by PhpStorm.
 * User: lenvo
 * Date: 2018/7/22
 * Time: 1:09
 */

namespace app\merapi\service;




use app\common\service\Service;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Log;

class Merchant extends Service {
    protected $merchant = null;
    public function __construct()
    {
        parent::__construct();
        $this->merchant = new \app\merapi\model\MerInfo();
    }

    public function getBySumary() {
        //首先检索用户收藏的店铺，然后检索用户没有收藏的店铺
        $mem_id = session("mem_id");
        $mem_mer = [];
        $page = input("page");
        if(isset($mem_id) && !empty($mem_id) && (!isset($page) || $page == 1)) {
            try {
                $mem_mer = $this->merchant->alias("m")
                    ->join("MerCollect mc", "mc.mer_id = m.id")
                    ->join("MerStore ms", "ms.mer_id = m.id")
                    ->where("ms.valid", 1)
                    ->where("mc.mem_id", $mem_id)
                    ->field("m.*,ms.photo1,ms.photo2,ms.photo3")->select();
            } catch (\Exception $e) {
                Log::write($e->getMessage());
            }

        }
        //提取已收藏的店铺ID
        $mer_ids = [];
        foreach ($mem_mer as $_) {
            array_push($mer_ids, $_['id']);
        }
        $sorted = $this->merchant->alias("m")
            ->join("MerStore ms", "ms.mer_id = m.mer_id")
            ->join("Area p1","m.mer_province = p1.id","LEFT")
            ->join("Area p2","m.mer_city = p2.id","LEFT")
            ->join("Area p3","m.mer_area = p3.id","LEFT")
            ->where("ms.valid", 1)
            ->order("m.level desc,m.score1 desc,m.score2 desc,m.score3 desc,m.star_num desc")
            ->field("m.*,ms.photo1,ms.photo2,ms.photo3,p1.name as province,p2.name as city,p3.name as area")
            ->where("m.id", "not in", implode(",", $mer_ids))
            ->paginate(15);



        return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
            ->setData("merchants",$sorted);
    }

    public function getByDist() {
        $sorted = $this->merchant->alias("m")
            ->join("MerStore ms", "ms.mer_id = m.id")
            ->join("Area p1","m.mer_province = p1.id","LEFT")
            ->join("Area p2","m.mer_city = p2.id","LEFT")
            ->join("Area p3","m.mer_area = p3.id","LEFT")
            ->where("ms.valid", 1)
            ->field("m.*,ms.photo1,ms.photo2,ms.photo3,p1.name as province,p2.name as city,p3.name as area")
                ->paginate(15);

        return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
            ->setData("merchants",$sorted);
    }

    public function getById($id) {
        $m = $this->merchant->alias("m")
            ->join("MerStore ms", "ms.mer_id = m.mer_id")
            ->join("Area p1","m.mer_province = p1.id","LEFT")
            ->join("Area p2","m.mer_city = p2.id","LEFT")
            ->join("Area p3","m.mer_area = p3.id","LEFT")
            ->where("ms.valid", 1)
            ->field("m.*,ms.photo1,ms.photo2,ms.photo3,p1.name as province,p2.name as city,p3.name as area")
            ->where("m.mer_id",$id)
            ->find();
        return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
            ->setData("info",$m);
    }

    public function getBySale() {
        $sorted = $this->merchant->alias("m")
            ->join("MerStore ms", "ms.mer_id = m.id")
            ->join("Area p1","m.mer_province = p1.id","LEFT")
            ->join("Area p2","m.mer_city = p2.id","LEFT")
            ->join("Area p3","m.mer_area = p3.id","LEFT")
            ->where("ms.valid", 1)
            ->field("m.*,ms.photo1,ms.photo2,ms.photo3,p1.name as province,p2.name as city,p3.name as area")
            ->order("m.sale_num desc")
            ->paginate(15);
        return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
            ->setData("merchants",$sorted);
    }

}